import 'package:equatable/equatable.dart';

/// 用户数据模型
class UserModel extends Equatable {
  final int id;
  final String username;
  final String email;
  final String role;
  final String? orgName;
  final String? avatarUrl;
  final bool isActive;
  final DateTime createTime;
  final DateTime? updateTime;

  const UserModel({
    required this.id,
    required this.username,
    required this.email,
    required this.role,
    this.orgName,
    this.avatarUrl,
    required this.isActive,
    required this.createTime,
    this.updateTime,
  });

  /// 从JSON创建实例
  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] as int,
      username: json['username'] as String,
      email: json['email'] as String,
      role: json['role'] as String,
      orgName: json['orgName'] as String?,
      avatarUrl: json['avatarUrl'] as String?,
      isActive: json['isActive'] as bool? ?? true,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: json['updateTime'] != null
          ? DateTime.parse(json['updateTime'] as String)
          : null,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'role': role,
      'orgName': orgName,
      'avatarUrl': avatarUrl,
      'isActive': isActive,
      'createTime': createTime.toIso8601String(),
      'updateTime': updateTime?.toIso8601String(),
    };
  }

  /// 创建副本
  UserModel copyWith({
    int? id,
    String? username,
    String? email,
    String? role,
    String? orgName,
    String? avatarUrl,
    bool? isActive,
    DateTime? createTime,
    DateTime? updateTime,
  }) {
    return UserModel(
      id: id ?? this.id,
      username: username ?? this.username,
      email: email ?? this.email,
      role: role ?? this.role,
      orgName: orgName ?? this.orgName,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      isActive: isActive ?? this.isActive,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
    );
  }

  /// 获取角色显示名称
  String get roleDisplayName {
    switch (role) {
      case 'ADMIN':
        return '系统管理员';
      case 'DEVELOPER':
        return '开发人员';
      case 'DEMANDER':
        return '需求方';
      case 'TESTER':
        return '测试人员';
      case 'DBA':
        return '数据库管理员';
      default:
        return role;
    }
  }

  /// 获取用户显示名称（优先显示用户名，如果为空则显示邮箱）
  String get displayName {
    return username.isNotEmpty ? username : email;
  }

  @override
  List<Object?> get props => [
        id,
        username,
        email,
        role,
        orgName,
        avatarUrl,
        isActive,
        createTime,
        updateTime,
      ];

  @override
  String toString() {
    return 'UserModel{id: $id, username: $username, email: $email, role: $role}';
  }
}

/// 登录响应模型
class LoginResponse extends Equatable {
  final String token;
  final String refreshToken;
  final String tokenType;
  final int expiresIn;
  final UserInfo user;

  const LoginResponse({
    required this.token,
    required this.refreshToken,
    required this.tokenType,
    required this.expiresIn,
    required this.user,
  });

  factory LoginResponse.fromJson(Map<String, dynamic> json) {
    return LoginResponse(
      token: json['token'] as String,
      refreshToken: json['refreshToken'] as String,
      tokenType: json['tokenType'] as String,
      expiresIn: json['expiresIn'] as int,
      user: UserInfo.fromJson(json['user'] as Map<String, dynamic>),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'token': token,
      'refreshToken': refreshToken,
      'tokenType': tokenType,
      'expiresIn': expiresIn,
      'user': user.toJson(),
    };
  }

  @override
  List<Object?> get props => [token, refreshToken, tokenType, expiresIn, user];
}

/// 用户信息模型（登录响应中的用户信息）
class UserInfo extends Equatable {
  final int id;
  final String username;
  final String email;
  final String role;
  final String? orgName;
  final String? avatarUrl;

  const UserInfo({
    required this.id,
    required this.username,
    required this.email,
    required this.role,
    this.orgName,
    this.avatarUrl,
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    return UserInfo(
      id: json['id'] as int,
      username: json['username'] as String,
      email: json['email'] as String,
      role: json['role'] as String,
      orgName: json['orgName'] as String?,
      avatarUrl: json['avatarUrl'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'role': role,
      'orgName': orgName,
      'avatarUrl': avatarUrl,
    };
  }

  /// 转换为UserModel
  UserModel toUserModel() {
    return UserModel(
      id: id,
      username: username,
      email: email,
      role: role,
      orgName: orgName,
      avatarUrl: avatarUrl,
      isActive: true,
      createTime: DateTime.now(),
    );
  }

  @override
  List<Object?> get props => [id, username, email, role, orgName, avatarUrl];
}

/// 登录请求模型
class LoginRequest extends Equatable {
  final String username;
  final String password;
  final bool rememberMe;

  const LoginRequest({
    required this.username,
    required this.password,
    this.rememberMe = false,
  });

  factory LoginRequest.fromJson(Map<String, dynamic> json) {
    return LoginRequest(
      username: json['username'] as String,
      password: json['password'] as String,
      rememberMe: json['rememberMe'] as bool? ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'username': username,
      'password': password,
      'rememberMe': rememberMe,
    };
  }

  @override
  List<Object?> get props => [username, password, rememberMe];
}
